package cn.lyvust.eais.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.lyvust.eais.common.annotation.SysLogs;
import cn.lyvust.eais.common.bean.ResponseCode;
import cn.lyvust.eais.common.bean.ResponseResult;
import cn.lyvust.eais.common.exception.RequestException;
import cn.lyvust.eais.common.util.BeansUtil;
import cn.lyvust.eais.manage.dto.param.evaluation.ProjectItemRecordParam;
import cn.lyvust.eais.manage.dto.result.ProjectItemRecordStatResp;
import cn.lyvust.eais.manage.entity.EvaluationProjectItemRecord;
import cn.lyvust.eais.manage.service.EvaluationProjectItemRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * @author ggzheng
 * @since 2021-01-21
 */
@Api(tags = {"指标项记录"})
@RestController
@RequestMapping("/evaluation/project_item_record")
public class EvaluationProjectItemRecordController {
    @Autowired
    private EvaluationProjectItemRecordService evaluationProjectItemRecordService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询指标项记录")
    public ResponseResult list(@RequestBody ProjectItemRecordParam param){
        return ResponseResult.e(ResponseCode.OK, evaluationProjectItemRecordService.listPage(param));
    }

    @PostMapping(value = {"/stat"})
    @ApiOperation(value = "统计指标项记录")
    public ResponseResult<ProjectItemRecordStatResp> stat(@RequestBody ProjectItemRecordParam param){
        return ResponseResult.e(ResponseCode.OK, evaluationProjectItemRecordService.stat(param));
    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "新增一个指标项记录")
    @SysLogs("新增一个指标项记录")
    public ResponseResult add(@RequestBody ProjectItemRecordParam param){
        return ResponseResult.e(ResponseCode.OK, evaluationProjectItemRecordService.add(param));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改一个指标项记录")
    @SysLogs("修改一个指标项记录")
    public ResponseResult update(@RequestBody ProjectItemRecordParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,指标项记录主键ID不能为空!");
        }
        if (StringUtils.isEmpty(param.getUpdateBy())) {
            throw RequestException.fail("修改失败,修改操作人姓名不能为空!");
        }
        EvaluationProjectItemRecord entity = evaluationProjectItemRecordService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, evaluationProjectItemRecordService.updateById(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除一个指标项记录")
    @SysLogs("删除一个指标项记录")
    public ResponseResult delete(@RequestBody ProjectItemRecordParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,指标项主键ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, evaluationProjectItemRecordService.removeById(param.getId()));
    }
}
